﻿using System;
using System.Collections.Generic;

public class FormulaGenerics
{
    #region init
    public static String FORMULA = "Formula";
    public static String REJECTED = "RejectionRule";
    public static String SUBRULE = "SubRule";
    #endregion

    #region LHS details
    public static String getLHSListByType(String type)
    {
        String query = "SELECT RowNo, Name FROM Formula WHERE type = '" + type + "'";
        //if (type.Equals(REJECTED) || type.Equals(SUBRULE))
        //{
        //    query += " AND toDate >= '" + DateTime.Now.ToString(" + GlobalVariables.DateFormat + ") + "'";
        //}
        query += "AND RowNo <> '20' ORDER BY Name";
        return query;
    }

    public static String getDetailsByType(int RowNo)
    {
        String query = "SELECT Name, SubType, FromDate, ToDate, ReturnType, WhenPart, ThenPart, GroupByPart";
        query += " FROM Formula WHERE RowNo = '" + RowNo + "'";
        return query;
    }
    #endregion

    #region populate page data
    public static String getListBySubType(String SubType, String formulaId)
    {
        String query = "SELECT Name as Value FROM Formula WHERE type = 'Formula' AND RowNo <> '20' AND SubType IN ( '" + SubType + "'";

        //dont add generic twice or for rejected there is only carsalesdata table so cant really display generic formulas
        if (SubType.Equals(REJECTED) || SubType.Equals("Generic"))
            query += " ) ";
        else
            query += " , 'Generic' ) ";

        //remove recursion - same formula not used in its own definition
        if (!String.IsNullOrEmpty(formulaId))
            query += " AND RowNo <> '" + formulaId.ToString() + "'";

        return (query);
    }

    public static String getFunctionList()
    {
        return "SELECT Value FROM GenericTable WHERE Category = 'Formula' AND Name LIKE 'SQL%' ORDER BY Value";
    }

    public static String getQueriableColumnList(String page)
    {
        //if (page.Equals(REJECTED))
        //    return "Select ColumnName AS Value FROM ColumnDetails WHERE Queryable = 'Yes' AND TableName = 'CarSalesData'";
        //else
        return "Select ( TableName+'.'+ColumnName ) AS Value FROM ColumnDetails WHERE Queryable = 'Yes'";
    }

    public static String getSlabsList()
    {
        return "SELECT Name As Value FROM Slabs";
    }

    /*get the list of all the columns in db
     SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
        FROM sys.columns AS c
        WHERE OBJECT_NAME(c.OBJECT_ID) not like 'sys%' AND OBJECT_NAME(c.OBJECT_ID) not like 'queue%' AND OBJECT_NAME(c.OBJECT_ID) not like 'file%'
        ORDER BY OBJECT_NAME(c.OBJECT_ID), c.name  
     */

    public static String getReturnTypes()
    {
        return "SELECT DISTINCT [Value] FROM [GenericTable] WHERE (([Category] = 'Formula') AND ([Name] = 'DataType')) ORDER BY [Value]";
    }

    public static String getSubType(String page)
    {
        if (page.Equals(SUBRULE))
            return "SELECT DISTINCT [Value] FROM [GenericTable] WHERE (([Category] = 'Formula') AND ([Name] = 'SubRuleType')) ORDER BY [Value]";
        else
            return "SELECT DISTINCT [Value] FROM [GenericTable] WHERE (([Category] = 'Formula') AND ([Name] = 'SubType')) ORDER BY [Value]";
    }
    #endregion

    #region page specific
    public static Boolean checkIfRequiredOnPage(String page, String element)
    {
        if (page.Equals(FORMULA))
        {
            List<String> requierd = new List<String> { "ReturnType", "SubType", "ThenPart", "FunctionList", "FieldList" };
            List<String> notRequierd = new List<String> { "FromDate", "ToDate", "WhenPart", "GroupByPart", "FormulaList", "SlabsList" };
            if (requierd.Contains(element)) return true;
            else if (notRequierd.Contains(element)) return false;
        }
        else if (page.Equals(REJECTED))
        {
            List<String> requierd = new List<String> { "FromDate", "ToDate", "WhenPart", "FunctionList", "FormulaList", "FieldList" };
            List<String> notRequierd = new List<String> { "ReturnType", "SubType", "ThenPart", "GroupByPart", "SlabsList" };
            if (requierd.Contains(element)) return true;
            else if (notRequierd.Contains(element)) return false;
        }
        else if (page.Equals(SUBRULE))
        {
            List<String> requierd = new List<String> { "FromDate", "ToDate", "WhenPart", "ThenPart", "GroupByPart", "FormulaList", "FieldList", "SlabsList", "SubType" };
            List<String> notRequierd = new List<String> { "ReturnType", "FunctionList" };
            if (requierd.Contains(element)) return true;
            else if (notRequierd.Contains(element)) return false;
        }

        Logger.Show("Invalid Element name: - " + page + " - " + element + ". ");
        return false;
    }

    public static String getTitle(String title)
    {
        if (title.Equals(FORMULA)) return "Formula Definition";
        else if (title.Equals(REJECTED)) return "Rejection Rule";
        else if (title.Equals(SUBRULE)) return "SubRule Definition";
        return String.Empty;
    }
    #endregion
}
